#include"../Include/WebPage.h"
#include<regex>
#include<cctype>
using std::regex;
using std::smatch;
using std::stoi;
using std::string;
    size_t getByteNum_UTF8(const char byte)
{
int byteNum = 0;
for (size_t i = 0; i < 6; ++i)
{
if (byte & (1 << (7 - i)))
++byteNum;
else
break;
}
return byteNum == 0 ? 1 : byteNum;
}
 // 构造函数
    WebPage::WebPage()
    {

    }
    WebPage::WebPage(const vector<string>& queryWords,const string& doc)
    {
        processDoc(queryWords,doc);
    }
    // 获取文档的 docid
    int WebPage::getDocId() const
    {
        return this->_docId;
    }
    // 获取文档的标题
    string WebPage::getTitle() const
    {
        return this->_docTitle;
    }
    // 获取文档的url
    string WebPage::getURL() const 
    {
        return this->_docUrl;
    }
    //获取文档内容
    string WebPage::getContent() const
    {
        return this->_docContent;
    }
    //获取文档摘要
    string WebPage::getSummary() const
    {
        return this->_docSummary;
    }
    // 对格式化文档进行处理
    void WebPage::processDoc(const vector<string>& queryWords,const string& doc)
    {
        regex idPattern("<docid>(\\d+)</docid>");
    regex titlePattern("<title>(.+)</title>");
    regex urlPattern("<link>(.+)</link>");
    regex contentPattern("<content>(.{0,20000})</content>");
    regex descriptionPattern("<description>(.{0,20000})</description>");

    smatch matches;

    if (regex_search(doc, matches, idPattern)) 
    {
        _docId = stoi(matches[1].str());
    }
    else
    {
        _docId = 0;
    }


    if (regex_search(doc, matches, titlePattern)) 
    {
        _docTitle = matches[1].str();
    }
    else
    {
        _docTitle="";
    }

    if (regex_search(doc, matches, urlPattern)) 
    {
        _docUrl = matches[1].str();
    }
    else
    {
        _docUrl="";
    }

    if (regex_search(doc, matches, contentPattern)) 
    {
        if (!matches.empty())
        {
            _docContent = matches[1].str();
        }
        else
        {
            _docContent = "";
        }
    }
    else
    {
        _docContent="";
    }

    if (regex_search(doc, matches, descriptionPattern)) 
    {
        if (!matches.empty())
        {
             _docSummary = matches[1].str();
        }
        else
        {
            _docSummary = "";
        }
    }
    else
    {
        _docSummary="";
    }

    //提取摘要并标红
    string strTemp=_docContent+_docSummary;
     if(strTemp.size()==0)
     {
        return ;
     }
     _docSummary="";

 for(const auto& elem:queryWords)
     {
        size_t pos=strTemp.find(elem);

        
        int number=3;
        while(pos != std::string::npos && pos + 45< strTemp.length()&&number--)
        {

            int count=15;
            size_t num=0;
            while(count--)
            {
                num+=getByteNum_UTF8(strTemp[pos+num]);
            }
            _docSummary += strTemp.substr(pos, num);
            _docSummary += " ";

            pos=strTemp.find(elem,pos+elem.length());
        }

       
     }


    for(const auto& elem :queryWords)
    {
        string relapcement="\033[31m"+elem+"\033[0m";
        size_t pos=_docSummary.find(elem);
        while(pos!=std::string::npos)
        {
            _docSummary.replace(pos,elem.length(),relapcement);
            pos=_docSummary.find(elem,pos+relapcement.length());
        }
    }    
}

    